import { deepClone } from '@/util';
import { getRouters } from '@/api/resourceApi';
import Layout from '@/layout';
import EmptyLayout from '@/layout/empty';
import module_routers from '@/util/module-routers';

function hasPermission(permissions, route) {
    let isAuth = false;
    if (route.meta && route.meta.auth) {
        isAuth = permissions.some(auth => {
            if (typeof route.meta.auth == 'string') {
                return route.meta.auth === auth;
            } else {
                return route.meta.auth.some(routeAuth => {
                    return routeAuth === auth;
                });
            }
        });
    } else {
        isAuth = true;
    }
    return isAuth;
}

function filterAsyncRoutes(routes, permissions) {
    const res = [];
    routes.forEach(route => {
        const tmp = { ...route };
        if (hasPermission(permissions, tmp)) {
            if (tmp.children) {
                tmp.children = filterAsyncRoutes(tmp.children, permissions);
                tmp.children.length && res.push(tmp);
            } else {
                res.push(tmp);
            }
        }
    });
    return res;
}

// 将多层嵌套路由处理成平级
function flatAsyncRoutes(routes, breadcrumb, baseUrl = '') {
    let res = [];
    routes.forEach(route => {
        const tmp = { ...route };
        if (tmp.children) {
            let childrenBaseUrl = '';
            if (baseUrl == '') {
                childrenBaseUrl = tmp.path;
            } else if (tmp.path != '') {
                childrenBaseUrl = `${baseUrl}/${tmp.path}`;
            }
            let childrenBreadcrumb = deepClone(breadcrumb);
            if (route.meta.breadcrumb !== false) {
                childrenBreadcrumb.push({
                    path: childrenBaseUrl,
                    title: route.meta.title
                });
            }
            let tmpRoute = deepClone(route);
            tmpRoute.path = childrenBaseUrl;
            tmpRoute.meta.breadcrumbNeste = childrenBreadcrumb;
            delete tmpRoute.children;
            res.push(tmpRoute);
            let childrenRoutes = flatAsyncRoutes(tmp.children, childrenBreadcrumb, childrenBaseUrl);
            childrenRoutes.map(item => {
                // 如果 path 一样则覆盖，因为子路由的 path 可能设置为空，导致和父路由一样，直接注册会提示路由重复
                if (res.some(v => v.path == item.path)) {
                    res.forEach((v, i) => {
                        if (v.path == item.path) {
                            res[i] = item;
                        }
                    });
                } else {
                    res.push(item);
                }
            });
        } else {
            if (baseUrl != '') {
                if (tmp.path != '') {
                    tmp.path = `${baseUrl}/${tmp.path}`;
                } else {
                    tmp.path = baseUrl;
                }
            }
            // 处理面包屑导航
            let tmpBreadcrumb = deepClone(breadcrumb);
            if (tmp.meta.breadcrumb !== false) {
                tmpBreadcrumb.push({
                    path: tmp.path,
                    title: tmp.meta.title
                });
            }
            tmp.meta.breadcrumbNeste = tmpBreadcrumb;
            res.push(tmp);
        }
    });
    return res;
}

function generateTree(menuList) {
    let tree = [];
    let itemMap = {};

    let home = module_routers['dashboard'];
    tree.push(home);

    menuList.map(item => {

        // console.log("-----####------")
        // console.log(item.menuCode);
        let config = module_routers[item.menuCode];
        // console.log("---------------")
        // console.log(config);
        if (!config) {
            return;
            // routerConfig = {
            //     component: item.pid==1?'Layout':'EmptyLayout',
            //     name: item.name,
            //     path: '/'+item.code,
            //     meta: {
            //         title: item.name,
            //         icon: 'sidebar-breadcrumb',
            //     }
            // }
        }
        let routerConfig = deepClone(config);
        console.log("-----####------")
        console.log(routerConfig);

        if (routerConfig.solo) {
            let component = routerConfig.component;
            routerConfig.component = 'Layout';
            routerConfig.children = [{
                pid: item.parentId,
                id: item.menu_id,
                component: component,
                path: routerConfig.path + '/index',
                name: routerConfig.name + '-index',
                meta: routerConfig.meta
            }];
        }

        routerConfig.id = item.menu_id;
        routerConfig.pid = item.parentId;
        itemMap['id_' + item.menu_id] = routerConfig;

        if (item.parentId == 1) {
            tree.push(routerConfig);
        } else {
            let pitem = itemMap['id_' + item.parentId];
            if (pitem) {
                if (!('children' in pitem)) {
                    pitem.children = [];
                }
                pitem.children.push(routerConfig);
            }
        }
    });

    tree.forEach(item => {
        if (item.children) {
            item.children = item.children.sort(function(a, b) {
                return a.meta.seq - b.meta.seq;
            });
        }
    });

    console.log(tree);

    return tree.sort(function(a, b) {
        return a.meta.seq - b.meta.seq;
    });
}

// 遍历后台传来的路由字符串，转换为组件对象
function filterAsyncRouter(asyncRouterMap) {
    return asyncRouterMap.filter(route => {
        if (route.component) {
            // Layout组件特殊处理
            if (route.component === 'Layout') {
                route.component = Layout;
            } else if (route.component === 'EmptyLayout') {
                route.component = EmptyLayout;
            } else if (route.component) {
                route.component = loadView(route.component);
            }
        }
        if (route.children != null && route.children && route.children.length) {
            route.children = filterAsyncRouter(route.children);
        }
        return true;
    });
}

export const loadView = view => { // 路由懒加载
    return resolve =>  require([`@/${view}`], resolve);
};

const state = {
    isGenerate: false,
    routes: [],
    headerActived: 0
};

const getters = {
    sidebarRoutes: state => {
        return state.routes[state.headerActived].children;
    },
    isSolo: state => {
        return state.routes[state.headerActived].solo;
    }
};

const actions = {
    // 根据权限动态生成路由
    generateRoutes({rootState, dispatch, commit}, data) {
        // eslint-disable-next-line no-async-promise-executor
        return new Promise(async resolve => {
            // let accessedRoutes
            // // 判断权限功能是否开启
            // if (rootState.settings.openPermission) {
            //     const permissions = await dispatch('user/getPermissions', null, { root: true })
            //     accessedRoutes = filterAsyncRoutes(data.asyncRoutes, permissions)
            // } else {
            //     accessedRoutes = data.asyncRoutes
            // }

            const res = await getRouters({
                role_id: rootState.user.role_id
            });
            let menuTruee = generateTree(res.res.Menu);
            let accessedRoutes = filterAsyncRouter(menuTruee);

            commit('setRoutes', accessedRoutes);
            commit('setHeaderActived', data.currentPath);
            let routes = accessedRoutes;

            // console.log(routes);

            if (rootState.settings.enableFlatRoutes) {
                routes.map(item => {
                    if (item.children) {
                        item.children = flatAsyncRoutes(item.children, [{
                            path: item.path,
                            title: item.meta.title
                        }]);
                    }
                });
            }
            resolve(routes);
        });
    }
};

const mutations = {
    invalidRoutes(state) {
        state.isGenerate = false;
        state.headerActived = 0;
    },
    setRoutes(state, routes) {
        state.isGenerate = true;
        let newRoutes = deepClone(routes);
        state.routes = newRoutes;
    },
    // 根据路由判断属于哪个头部导航
    setHeaderActived(state, path) {
        state.routes.map((item, index) => {
            if (
                path == item.path || (item.children && item.children.some(r => {
                    return path.indexOf(r.path + '/') === 0 || path == r.path;
                }))
            ) {
                state.headerActived = index;
            }
        });
    },
    // 切换头部导航
    switchHeaderActived(state, index) {
        state.headerActived = index;
    }
};

export default {
    namespaced: true,
    state,
    actions,
    getters,
    mutations
};
